﻿using Convertor.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Wpf.CommonTools.SystemExtension;


namespace Convertor.Plugin.FindAllFiles
{
    public class Plugin : PluginBase
    {
        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.FindAllFiles;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.FindAllFiles;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_File") + " - " + UILanguage.FindResource("Plugin_FindAllFile_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_FindAllFile_Desc"); }
        }

        

        

        #endregion

        #region IConvertParamters 成员

        List<string> sb;
        public override IConvertParamters Output(IConvertParamters content)
        {
            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }

            string path = content.OutputContent.ToString().Trim();
            sb = new List<string>();
            if (!Directory.Exists(path))
            {
                if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_FindAllFile_Result_PathNotExist"), path), LogTypes.Alert | LogTypes.HidePluginName); }
                return content;
            }
            else
            {
                if (config.FindType.HasFlag(FindTypes.FindFiles))
                {
                    string[] lll = Directory.GetFiles(path);
                    foreach (string f in lll)
                    {
                        sb.Add(f);
                    }
                }

                if (config.FindType.HasFlag(FindTypes.FindSubFolder))
                {
                    DirSearch(path);
                }
                else
                {
                    if (config.FindType.HasFlag(FindTypes.FindFolder))
                    {
                        string[] nnn = Directory.GetDirectories(path);
                        foreach (string f in Directory.GetDirectories(path))
                        {
                            sb.Add(f);
                        }
                    }
                }
                if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_FindAllFile_ConvertSuccess"), sb.Count), LogTypes.Normal | LogTypes.HidePluginName); }
                return new ConvertParamters(content.LastInputContent, sb);
            }
        }

        void DirSearch(string sDir)
        {
            foreach (string d in Directory.GetDirectories(sDir))
            {
                if (config.FindType.HasFlag(FindTypes.FindFolder))
                {
                    sb.Add(d);
                }
                if (config.FindType.HasFlag(FindTypes.FindFiles))
                {
                    foreach (string f in Directory.GetFiles(d))
                    {
                        sb.Add(f);
                    }
                }
                DirSearch(d); //递归查询 调用自己
            }
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
